package com.wq.feign.provider.start;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProviderInit implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        this.initFlowQPSRule();
    }

    private void initFlowQPSRule() {
        List<FlowRule> rules = new ArrayList<>();
        String resourceName = "/oauth/publicKey";
        FlowRule flowRule = new FlowRule(resourceName);
        flowRule.setCount(1);
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setLimitApp("default");
        rules.add(flowRule);
        FlowRuleManager.loadRules(rules);
    }
}
